package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    public List<Setmeal> search(Integer categoryId) {
        List<Setmeal> setList;
        setList = setmealMapper.setSearch(categoryId);
        return setList;
    }


    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void addSet(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.addSet(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (int i = 0; i < setmealDishes.size(); i++) {
            setmealMapper.setDishs(setmealDishes.get(i), setmeal.getId());
        }
    }


    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public Long pageSearchCount(SetmealPageQueryDTO setmealPageQueryDTO) {
        Long count = setmealMapper.count(setmealPageQueryDTO);
        return count;
    }

    public List<SetmealVO> pageSearchList(SetmealPageQueryDTO setmealPageQueryDTO) {
        Long start = (long) ((setmealPageQueryDTO.getPage() - 1) * setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealList = setmealMapper.PageSearch(setmealPageQueryDTO, start);
        return setmealList;
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getByid(Integer id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = new Setmeal();
        setmeal = setmealMapper.getByid(id);//取出基本套餐信息不包含关联菜品信息
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = new ArrayList<>();
        setmealDishes = setmealMapper.getByDishID(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    /**
     * 套餐启售停售
     *
     * @param status
     * @param id
     * @return
     */
    public void ChangeStatus(Integer status, Integer id) {
        List<Integer> count = setmealMapper.getDontStatusDish(id);
        for (int i = 0; i < count.size(); i++) {
            if (count.get(i) == 0) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.ChangeStatus(status, id);
    }


    /**
     * 套餐修改
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void changeSet(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.changeSet(setmeal);
        List<SetmealDish> setmealDishes = new ArrayList<>();
        setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.chanDishSet(setmealDTO.getId());//全删了重插
        //log.info("套餐关联菜品信息{}", setmealDishes);
        Long setid = setmealDTO.getId();
        for (int i = 0; i < setmealDishes.size(); i++) {
            setmealDishes.get(i).setSetmealId(setid);
            setmealMapper.addSetDish(setmealDishes.get(i));
        }
    }

    @Transactional
    public void DropSetmeal(Integer[] ids) {
        int status = setmealMapper.SearchSetStatus(ids);
        if (status == 1) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.DropSetmeal(ids);
        setmealMapper.DropSetmealDish(ids);
    }


    /**
     * 根据套餐id查询菜品信息
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getBySetmealId(Integer id) {
        List<DishItemVO> dishItemVOList = new ArrayList<>();
        dishItemVOList = setmealMapper.getBySetmealId(id);
        return dishItemVOList;
    }


}
